package com.lecture.java8.datastructure;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2021/3/24 20:12
 */
public class Demo4 {

    public static void main(String[] args) {
        int[] a = {1,-5,8, 3,-4,15,-8};
        fun1(a, a.length);
        fun2(a, a.length);
    }

    public static void fun1(int[] a, int n) {
        int thisSum, maxSum = 0;

        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                thisSum = 0;
                for (int k = i; k < j; k++) {
                    thisSum += a[k];
                }
                if (thisSum > maxSum) {
                    maxSum = thisSum;
                }
            }
        }
        System.out.println(maxSum);
    }

    public static void fun2(int[] a, int n) {
        int thisSum, maxSum = 0;
        for (int i = 0; i < n; i++) {//i是最左端的位置
            thisSum = 0;
            for (int j = i; j < n; j++) {//j是最右端的位置
                thisSum += a[j];
                if (thisSum > maxSum) {
                    maxSum = thisSum;
                }
            }
        }
        System.out.println(maxSum);
    }
}
